package com.zhanbo.zhanbowymusic.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.utils.PixelUtil;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.main.featured.adapter.FeaturedVIPHitListAdapter;
import com.zhanbo.zhanbowymusic.music.MusicController;
import com.zhanbo.zhanbowymusic.play.PlayListAdapter;

import java.util.ArrayList;
import java.util.List;

public class PlayListDialog extends Dialog {
    private Context mContext;
    private RecyclerView rv_dialog_play_list;
    private PlayListAdapter playListAdapter;
    private TextView tv_title_num;
    private LinearLayout ll_dialog_qingkong;


    public PlayListDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext =context;
        Window window = getWindow();

        if (window != null) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = PixelUtil.dp2px(mContext,450);
            layoutParams.height = PixelUtil.dp2px(mContext,500);
            layoutParams.y= PixelUtil.dp2px(mContext,160);
            window.setBackgroundDrawableResource(R.color.transparency);
            window.setAttributes(layoutParams);
            window.setGravity(Gravity.RIGHT|Gravity.BOTTOM);
        }
        setCanceledOnTouchOutside(true);


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_play_list);
        initView();
        initData();
    }

    private void initView() {
        rv_dialog_play_list=   findViewById(R.id.rv_dialog_play_list);
        ll_dialog_qingkong=   findViewById(R.id.ll_dialog_qingkong);
        tv_title_num = findViewById(R.id.tv_title_num);

    }

    private void initData() {
        rv_dialog_play_list.setLayoutManager(new GridLayoutManager(mContext,1));
         playListAdapter = new PlayListAdapter(mContext);
        rv_dialog_play_list.setAdapter(playListAdapter);
        playListAdapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<SongBean>() {
            @Override
            public void onItemClick(int position) {
                MusicController.getInstance().updatePosition(position);
            }
        });
        ll_dialog_qingkong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicController.getInstance().getSheetData().clear();
                dismiss();
            }
        });
    }

    @Override
    public void show() {
        super.show();
        List<SongBean> data = MusicController.getInstance().getSheetData();
        if (playListAdapter.getItems()!=null){
            playListAdapter.getItems().clear();
            playListAdapter.getItems().addAll(data);
            playListAdapter.notifyDataSetChanged();
            tv_title_num.setText(data.size()+"");
        }
    }
}
